package com.hospital.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hospital.mapper.ZyxxbMapper;
import com.hospital.pojo.Zyxxb;
import com.hospital.vo.ZhuYuanQueryVo;
import com.hospital.vo.ZhuYuanResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZhuYuanService {
    @Resource
    private ZyxxbMapper zyxxbMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<ZhuYuanResultVo> getAllZyxx(Integer pageNum, Integer pageSize, ZhuYuanQueryVo zhuYuanQueryVo){
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("zysj desc");
        List<ZhuYuanResultVo> list = zyxxbMapper.getZyxxList(zhuYuanQueryVo);
        return new PageInfo<>(list);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public ZhuYuanResultVo getZyxxDetail(Integer blh){
        ZhuYuanQueryVo zhuYuanQueryVo = new ZhuYuanQueryVo();
        zhuYuanQueryVo.setBlh(blh);
        List<ZhuYuanResultVo> list = zyxxbMapper.getZyxxList(zhuYuanQueryVo);
        return list.get(0);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateZyxxb(Zyxxb zyxxb){
        int update = zyxxbMapper.updateByPrimaryKeySelective(zyxxb);
        return update;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addZyxx(Zyxxb zyxxb) {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        zyxxb.setZysj(new Date());
        zyxxb.setJfyj(2000.00);
        Zyxxb zyxxb1 = zyxxbMapper.selectByPrimaryKey(zyxxb.getBlh());
        if (zyxxb1 != null) return -1;
        int i = zyxxbMapper.insertSelective(zyxxb);
        return i;
    }
}
